
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

// 硬件驱动头文件
#include "hal_bsp_ssd1306.h"
#include "hal_bsp_sht20.h"

#define MONITOR_TASK_MEMORY (4 * 1024)    // 任务内存分配
#define MONITOR_TASK_PRIORITY 25         // 任务优先级
#define SENSOR_POLL_DELAY 1000           // 传感器轮询间隔(ms)
#define DISPLAY_UPDATE_DELAY 1000        // 显示刷新间隔(ms)

// 环境数据存储
static float envTemperature = -99.0f;
static float envHumidity = -99.0f;

// 传感器数据采集线程
static void EnvDataCollector(void *param)
{
    (void)param;
    printf("Environment Data Collector started.\n");

    // 初始化温湿度传感器
    SHT20_Initialize(); 

    while (1) {
        float currentTemp = 0.0f;
        float currentHumid = 0.0f;
      
        // 获取最新环境数据
        SHT20_GetMeasurements(&currentTemp, &currentHumid);
      
        envTemperature = currentTemp;
        envHumidity = currentHumid;

        osDelay(SENSOR_POLL_DELAY);
    }
}

// 显示刷新线程
static void DisplayUpdater(void *param)
{
    (void)param;
    char tempDisplay[16] = {0};
    char humidDisplay[16] = {0};
    float displayTemp, displayHumid;

    printf("Display Updater started.\n");

    // 初始化显示设备
    SSD1306_Initialize();
    SSD1306_ClearScreen();

    // 绘制静态界面元素
    SSD1306_DrawString(0, 0, (uint8_t*)"Environment Data", 16);
    SSD1306_DrawString(0, 1, (uint8_t*)"Temperature:", 16);
    SSD1306_DrawString(0, 2, (uint8_t*)"Humidity:", 16);
    SSD1306_DrawString(90, 1, (uint8_t*)"°C", 16);
    SSD1306_DrawString(90, 2, (uint8_t*)"%", 16);

    while(1) {
        displayTemp = envTemperature;
        displayHumid = envHumidity;

        // 格式化显示内容
        if (displayTemp > -90.0f) {
             snprintf(tempDisplay, sizeof(tempDisplay), "%.2f", displayTemp);
        } else {
            snprintf(tempDisplay, sizeof(tempDisplay), "--.--");
        }
     
        if (displayHumid > -90.0f) {
             snprintf(humidDisplay, sizeof(humidDisplay), "%.2f", displayHumid);
        } else {
             snprintf(humidDisplay, sizeof(humidDisplay), "--.--");
        }
      
        // 清除旧数据区域
        SSD1306_DrawString(40, 1, (uint8_t*)"       ", 16); 
        SSD1306_DrawString(40, 2, (uint8_t*)"       ", 16); 

        // 更新显示内容
        SSD1306_DrawString(40, 1, (uint8_t *)tempDisplay, 16);
        SSD1306_DrawString(40, 2, (uint8_t *)humidDisplay, 16);

        osDelay(DISPLAY_UPDATE_DELAY);
    }
}

// 系统初始化入口
static void EnvMonitorSystemInit(void)
{
    osThreadAttr_t dataCollectorConfig = {0};
    osThreadAttr_t displayUpdaterConfig = {0};

    // 配置数据采集线程
    dataCollectorConfig.name = "EnvDataCollector";
    dataCollectorConfig.stack_size = MONITOR_TASK_MEMORY;
    dataCollectorConfig.priority = MONITOR_TASK_PRIORITY;

    // 配置显示更新线程
    displayUpdaterConfig.name = "DisplayUpdater";
    displayUpdaterConfig.stack_size = MONITOR_TASK_MEMORY;
    displayUpdaterConfig.priority = MONITOR_TASK_PRIORITY;

    // 创建系统线程
    if (osThreadNew(EnvDataCollector, NULL, &dataCollectorConfig) == NULL) {
        printf("[EnvMonitor] Failed to initialize data collector!\n");
    }

    if (osThreadNew(DisplayUpdater, NULL, &displayUpdaterConfig) == NULL) {
        printf("[EnvMonitor] Failed to initialize display updater!\n");
    }
}

// 注册系统初始化
APP_FEATURE_INIT(EnvMonitorSystemInit);
